<?php
namespace app\api\controller;

use app\api\controller\Sample;
use app\common\controller\ApiController;
use think\App;
use \app\admin\traits\Curd;
use think\facade\Db;
use app\api\controller\Adv;
use app\api\model\User as ModelUser;
use dh2y\qrcode\QRcode;
use PhpOffice\PhpSpreadsheet\Reader\Xls\MD5;

class Shop extends ApiController
{
    protected $noNeedLogin = [
        "get_banner_list",
        "get_demo_list",
        "get_qrcode",
        "login_token",
        "get_url",
        "fixedbag_editnum"
    ];
    protected $mustLogin = [];
    protected $counter = 0;

    use \app\admin\traits\Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    //轮播图列表
    public function get_banner_list()
    {
        $param = $this->request->param();
        $num = isset($param['num'])?$param['num']:5;
        $list = Db::name("banner")->order('id desc')->paginate($num);
        $this->success('获取成功', $list);
    }

    //试玩列表
    public function get_demo_list()
    {
        $param = $this->request->param();
        $num = isset($param['num'])?$param['num']:10;
        $list = Db::name("classification")->where("status",0)->order('id desc')->field("id,title")->select()->toArray();
        foreach($list as $k=>$v){
            $demo = Db::name("demo")->where("class_id",$v["id"])->order('id desc')->field("id,demo_name,demo_img,price,jump_url")->paginate($num);
            $demo = $demo->toArray();
            if(!empty($demo["data"])){
                $v["demo"] = $demo;
            }else{
                $v["demo"] = [];
            }
            $list[$k] = $v;
        }
        $filteredArray = array_filter($list, function ($subArray) {
            return !in_array(null, $subArray) && !in_array('', $subArray) && count($subArray) > 0;
        });
        $this->success('获取成功', $filteredArray);
    }
    
    //获取二维码
    public function get_qrcode(){
        $code = new QRcode();
        $code->png("http://www.baidu.com");
        $url = $code->entry();
        $this->success('获取成功', $url);
    }

    //用户获取token
    public function login_token(){
        $param = $this->request->param();
        $rule = [
            'uuid|用户唯一识别'  => 'require',
        ];
        $this->validate($param, $rule);
        $token = Auth::makeToken();
        $flag = Db::name("h5user")->where("uuid",$param["uuid"])->find();
        if(empty($flag)){
            Db::name("h5user")->insert(["uuid"=>$param["uuid"],"create_time"=>time()]);
            $h5userId = Db::name("h5user")->getLastInsID();
            Db::name("user_token")->insert(["token"=>$token,"user_id"=>$h5userId,"type"=>$this->method]);
            $userinfo = Db::name("h5user")->where("id",$h5userId)->find();
            $this->success('登录成功', ['token' => $token, 'method' => $this->method, 'userinfo' => $userinfo]);
        }else{
            $user_token = Db::name("user_token")->where("user_id",$flag["id"])->find();
            if(empty($user_token)){
                Db::name("user_token")->insert(["token"=>$token,"user_id"=>$flag["id"],"type"=>$this->method]);
            }else{
                Db::name("user_token")->where("user_id",$flag["id"])->update(["token"=>$token]);
            }
            $this->success('登录成功', ['token' => $token, 'method' => $this->method, 'userinfo' => $flag]);
        }
       
    }

    //获取链接
    public function get_url(){
        $list = [
            "download_link"=>sysconfig('down','download_link'),
            "download_pageaddress"=>sysconfig('down','download_pageaddress')
        ];
        $this->success('获取成功', $list);
    }

    //固定包增加下载次数
    public function fixedbag_editnum(){
        Db::name("system_config")->where("name","amountof_downloads")->inc("value",1)->update();
        $this->success('操作成功');
    }


    //点击跳转
    public function  clickjump_count(){
        $param = $this->request->param();
        $rule = [
            'demo_id|试玩id'  => 'require',
            'type|类型'  => 'require',
        ];
        $this->validate($param, $rule);
        //类型  1点击  2跳转
        if($param["type"] == 1){
            Db::name("demo")->where("id",$param["demo_id"])->inc("click_count",1)->update();
            Db::name("clickjump_count")->insert([
                "uid"=>$this->uid,
                "type"=>1,
                "demo_id"=>$param["demo_id"]
            ]);
        }elseif($param["type"] == 2){
            $flag = Db::name("clickjump_count")->where("id",$param["demo_id"])->where("uid",$this->uid)->find();
            if(empty($flag)){
                Db::name("demo")->where("id",$param["demo_id"])->inc("jump_participant",1)->update();
                Db::name("clickjump_count")->insert([
                    "uid"=>$this->uid,
                    "type"=>2,
                    "demo_id"=>$param["demo_id"]
                ]);
            }
        }else{
            $this->error("类型不存在");
        }
        $this->success('操作成功');
    }
 
    
 
     
   
} 
  
   